%Scrpits to read the 720 x 1400 source file, down sample it to 180 x 360
%and write the data into a format which is compataible with Alexei's x3dg
%code. Note that this script does not read the .source file, but the text
%file contains the matrices extracted (cut and paste the ARRAY) from it.

fid1 = fopen('/data/backup/mnair/ocean/header.txt','rt');
fid = fopen('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling/tpxo72_O1_180_360.source','wt');

real_phi = load('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling/real_phi.txt');


real_phi_1440 = reshape(real_phi',[1440,720])';
[X_360,Y_360] = meshgrid(0.5:1:360,(-90+0.5):1:90);
[X_1440,Y_1440] = meshgrid(0.125:0.25:360,(-90+0.125):0.25:90);
real_phi_360 = interp2(X_1440,Y_1440,real_phi_1440,X_360,Y_360);
% put the data back into a matrix 5400 x 12
real_ph_360_write = reshape(real_phi_360',[12, 5400])';

           for ii = 1:34,

                s=fgetl(fid1);

                fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

for i = 1:5400,
    for j = 1:12,
      fprintf(fid,'%12.4e',  real_ph_360_write(i,j));
    end;
    fprintf(fid,'\n');
end;
 

real_phi = load('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling/imag_phi.txt');
real_phi_1440 = reshape(real_phi',[1440,720])';
real_phi_360 = interp2(X_1440,Y_1440,real_phi_1440,X_360,Y_360);
% put the data back into a matrix 5400 x 12
real_ph_360_write = reshape(real_phi_360',[12, 5400])';
% write the junk into a file

            fprintf(fid,'\n');

            for ii = 1:17,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);
            

for i = 1:5400,
    for j = 1:12,
      fprintf(fid,'%12.4e',  real_ph_360_write(i,j));
    end;
    fprintf(fid,'\n');
end;


real_phi = load('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling/real_theta.txt');
real_phi_1440 = reshape(real_phi',[1440,720])';
[X_360,Y_360] = meshgrid(0.5:1:360,(-90+0.5):1:90);
[X_1440,Y_1440] = meshgrid(0.125:0.25:360,(-90+0.125):0.25:90);
real_phi_360 = interp2(X_1440,Y_1440,real_phi_1440,X_360,Y_360);
% put the data back into a matrix 5400 x 12
real_ph_360_write = reshape(real_phi_360',[12, 5400])';
% write the junk into a file

            fprintf(fid,'\n');

            for ii = 1:17,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);


for i = 1:5400,
    for j = 1:12,
      fprintf(fid,'%12.4e',  real_ph_360_write(i,j));
    end;
    fprintf(fid,'\n');
end;



real_phi = load('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling/imag_theta.txt');
real_phi_1440 = reshape(real_phi',[1440,720])';
[X_360,Y_360] = meshgrid(0.5:1:360,(-90+0.5):1:90);
[X_1440,Y_1440] = meshgrid(0.125:0.25:360,(-90+0.125):0.25:90);
real_phi_360 = interp2(X_1440,Y_1440,real_phi_1440,X_360,Y_360);
% put the data back into a matrix 5400 x 12
real_ph_360_write = reshape(real_phi_360',[12, 5400])';
% write the junk into a file
            fprintf(fid,'\n');

            for ii = 1:17,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

  
for i = 1:5400,
    for j = 1:12,
      fprintf(fid,'%12.4e',  real_ph_360_write(i,j));
    end;
    fprintf(fid,'\n');
end;


            fprintf(fid,'\n');

            for ii = 1:37,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

            fclose(fid);

            fclose(fid1);



fclose all;

%% conductance map

 real_phi = load('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling/conductance_map_1440.txt');
 real_phi_1440 = reshape(real_phi',[1440,720])';
[X_360,Y_360] = meshgrid(0.5:1:360,(-90+0.5):1:90);
[X_1440,Y_1440] = meshgrid(0.125:0.25:360,(-90+0.125):0.25:90);
real_phi_360 = interp2(X_1440,Y_1440,real_phi_1440,X_360,Y_360);
% put the data back into a matrix 5400 x 12
real_ph_360_write = reshape(real_phi_360',[12, 5400])';
% write the junk into a file
fid = fopen('/nfs/satmag_work/mnair/projects/ocean/OBEM/tpxo72_180_360_modeling//conductance_map_360.txt','wt');

for i = 1:5400,
    for j = 1:12,
      fprintf(fid,'%12.4e',  real_ph_360_write(i,j));
    end;
    fprintf(fid,'\n');
end;
fclose(fid);      
clear;